import datetime
import os
import logging

from bookownershipservice import BookOwnershipService

from models import *
from google.appengine.ext import db
from google.appengine.api import users

class LoanService:
	def getLoansForOwner(self,owner):
		logging.info("Getting loans for owner %s", owner)
		return owner.loans.fetch(100)
		#return Loan.gql("WHERE owner = :owner",owner)
		
	def createLoan(self,_owner, _loaner, _book):
		logging.info("Created loan for owner=:owner, loaner=:loaner, book=:book",_owner,_loaner,_book)
		bookOwnership = BookOwnershipService().getBookOwnership(_owner,_book)
		loan = Loan(owner = _owner, loaner = _loaner, book = bookOwnership)
		loan.put()
		return loan
	
	def clear(self):
		loans = db.GqlQuery("SELECT * FROM Loan")
		for loan in loans:
			loan.delete()